var UpdateRestorationDatePopover = function () {
  var template = /* html */ `
    <div class="update-restoration-date-popover" v-popover:orderPopover v-loading.fullscreen.lock="loading">
      <div class="value-div">
        <slot></slot>
      </div>

      <el-popover v-model="showPopover" ref="orderPopover" placement="right" trigger="click" :visible-arrow="true" 
        title="修改复期" @show="toShowPopover">
        <order-form :purchase-order="purchaseOrder" style="width:310px;" @update-date="toUpdateDate"></order-form>
      </el-popover>
    </div>
  `;

  return {
    props: {
      order: {
        type: Object,
        required: true
      },
      orderType: {
        type: Number,
        default: 1
      }
    },

    data: function () {
      return {
        loading: false,
        showPopover: false,
        purchaseOrder: null
      };
    },

    methods: {
      toShowPopover: function () {
        this.purchaseOrder = _.cloneDeep(this.order);
      },

      toUpdateDate: function (laterPhaseDateList) {
        var order = this.order;
        var postData = order.getUpdateObject(laterPhaseDateList);
        if (typeof postData === 'string') {
          this.$message(this.getErrorMsg(postData));
          return;
        }

        var orderType = this.orderType;
        var requestUrl;
        if (orderType === 3) {
          requestUrl = 'advancePurchase/setLaterPhaseData';
        } else {
          postData.type = orderType;
          requestUrl = 'purchaseOrder/updatePurchaseLaterPhaseByPurchaseDetailId';
        }
        
        this.loading = true;
        var self = this;
        ajaxRequest(requestUrl, postData).then(function () {
          self.loading = false;
          order.setLaterPhaseDateList(laterPhaseDateList);
          self.showPopover = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      getErrorMsg: function (code) {
        var obj = {
          laterPhaseOne: "请选择预计收获期",
          laterPhaseTwo: "请选择复期1",
          laterPhaseThr: "请选择复期2",
          laterPhaseFour: "请选择复期3"
        };
        return obj[code];
      }
    },

    components: {
      OrderForm: OrderForm
    },

    template: template
  };
}();